package hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import bean.PublicationBean;

public class PublicationRepository extends GenericRepository<PublicationBean, Integer> implements Serializable {

	private static final long serialVersionUID = 1L;

	public PublicationBean GetById(Integer Id){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(PublicationBean.class).add(Restrictions.eq("id_message", Id));
		PublicationBean msg = (PublicationBean) criteria.uniqueResult();
		trans.commit();
		return msg;
	}

	public List<PublicationBean> GetAll(){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(PublicationBean.class);
		@SuppressWarnings("unchecked")
		List<PublicationBean> lst = (List<PublicationBean>) criteria.list();
		trans.commit();
		return lst ;
	}


	public List<PublicationBean> GetAllPublicationByDestinataire(int id_destinataire){
		Session s = HelperSessionFactory.getSession();
		try {
			Transaction trans = s.beginTransaction();
			Criteria criteria = s.createCriteria(PublicationBean.class).add(Restrictions.eq("id_eleve_destinataire", id_destinataire)).add(Restrictions.eq("privacy", false));
			@SuppressWarnings("unchecked")
			List<PublicationBean> lst = (List<PublicationBean>) criteria.list();
			trans.commit();
			return lst;
		}
		catch (RuntimeException r){
			s.getTransaction().rollback();
			throw r;
		}

	}
}
